package com.alibaba.citrus.ots.platform.enhance.functions.inventoryAllot.factory;

import com.alibaba.citrus.ots.common.exception.FunctionException;
import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.functions.inventoryAllot.entity.InventoryAllotLineEntity;
import com.alibaba.citrus.ots.platform.enhance.repository.InventoryAllotLineRepository;
import com.epoch.app.otsplatformenhance.model.dto.InventoryAllotLineSDO;
import com.epoch.app.otsplatformenhance.model.dto.UpdateDistributionQuantityRequest;
import com.epoch.app.otsplatformenhance.model.dto.UpdateRemainingQuantityRequest;

import java.math.BigDecimal;

public class InventoryAllotLineFactory {

    private static InventoryAllotLineRepository inventoryAllotLineRepository;

    static {
        inventoryAllotLineRepository = SpringContextUtil.getBean(InventoryAllotLineRepository.class);
    }

    public static InventoryAllotLineEntity updateRemainingQuantity(UpdateRemainingQuantityRequest updateRemainingQuantityRequest) {
        InventoryAllotLineSDO inventoryAllotLineSDO = inventoryAllotLineRepository.queryById(updateRemainingQuantityRequest.getId());
        BigDecimal i = new BigDecimal(inventoryAllotLineSDO.getRemainingQuantity());
        BigDecimal j = new BigDecimal(updateRemainingQuantityRequest.getQuantity());
        inventoryAllotLineSDO.setRemainingQuantity(i.subtract(j) + "");
        return InventoryAllotLineEntity.of(inventoryAllotLineSDO);
    }

    public static InventoryAllotLineEntity updateDistributionQuantity(
        UpdateDistributionQuantityRequest updateDistributionQuantityRequest) {
        InventoryAllotLineSDO inventoryAllotLineSDO = inventoryAllotLineRepository.queryById(updateDistributionQuantityRequest.getId());

        //分配数量
        BigDecimal distributionQuantity = new BigDecimal(inventoryAllotLineSDO.getDistributionQuantity());
        //剩余数量
        BigDecimal remainingQuantity = new BigDecimal(inventoryAllotLineSDO.getRemainingQuantity());
        //入参要修改的数量
        BigDecimal requestQuantity = new BigDecimal(updateDistributionQuantityRequest.getDistributionQuantity());
//        if (distributionQuantity.compareTo(requestQuantity) == 1 && distributionQuantity.compareTo(remainingQuantity) == 1) {
//            throw new FunctionException("OTS-06-001-00-15-004");
//        }
        if (requestQuantity.compareTo(BigDecimal.ZERO)<0){
            throw new FunctionException("OTS-06-001-00-15-005");
        }

        inventoryAllotLineSDO.setDistributionQuantity(updateDistributionQuantityRequest.getDistributionQuantity());
        //if (distributionQuantity.compareTo(remainingQuantity) == 0) {
        //    inventoryAllotLineSDO.setRemainingQuantity(updateDistributionQuantityRequest.getDistributionQuantity());
       // }
        //分配数量>剩余数量
        //if (requestQuantity.compareTo(distributionQuantity) > -1 && distributionQuantity.compareTo(remainingQuantity) == 1) {
        //    BigDecimal i = requestQuantity.subtract(distributionQuantity).add(remainingQuantity);
         //   inventoryAllotLineSDO.setRemainingQuantity(i + "");
       // }
        BigDecimal usedQuantity = distributionQuantity.subtract(remainingQuantity);
        if (requestQuantity.compareTo(usedQuantity)>-1){
            inventoryAllotLineSDO.setRemainingQuantity(requestQuantity.subtract(usedQuantity)+"");
        }else {
            inventoryAllotLineSDO.setRemainingQuantity(0+"");
        }


        return InventoryAllotLineEntity.of(inventoryAllotLineSDO);
    }
}
